package com.itheima.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author ZhangyiA
 * @describe:
 * @date 2021/06/24
 */

public class MyInterceptor1 implements HandlerInterceptor {
    // 在目标方法执行之前执行true
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        // 逻辑 , 判断用户是否登录 ， 本质判断 session 中有没有 user
//        HttpSession session = request.getSession();
//        User user = (User) session.getAttribute("user");
//        if(user == null){
//            // 没有登录
//            response.sendRedirect(request.getContextPath() + "/login.jsp");
//            return false;
//        }
//        // 放行  访问目标资源
        return true;
    }

    // 在目标方法执行之后, 视图对象返回之前执行
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        System.out.println("postHandle......");
    }

    // 在整个流程执行完毕后执行
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        System.out.println("afterCompletion......");
    }
}
